package leetcode_day._2021._202108._2131;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/8/25 19:52
 * 所有可能的路径
 * 算法：深度优先搜索
 */
public class _25_797 {

    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        List<Integer> tmp = new ArrayList<>();
        tmp.add(0);
        dfs(graph, tmp, 0);
        return ans;
    }
    private void dfs(int[][] graph, List<Integer> tmp, int cur) {
        if (cur == graph.length - 1) {
            ans.add(new ArrayList<>(tmp));
            return;
        }
        for (int next : graph[cur]) {
            tmp.add(next);
            dfs(graph, tmp, next);
            tmp.remove(tmp.size() - 1);
        }
    }

}
